% Figure 4
% The difference between the implied and actual charge-off rates
% and the ratio of the implied to actual charge-off rate
% The upper panel shows the difference between the implied charge-off
% rate and the actual charge-off rate. The lower panel shows the ratio of
% the implied charge-off rate to the actual charge-off rate.

clear; clc;


% loa data
opts = delimitedTextImportOptions("NumVariables", 10);
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
opts.VariableNames = ["Year", "Month", "MPR", "PortfYld", "ExSpread", "ExSpreadQ", "Chargeoff_P", "Chargeoff_Q", "ChargeoffPremium", "QPChrgRateRatio"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
IndexCardPortfVars = readtable("Data_Figure_04.csv", opts);
clear opts
IndexCardPortfVars.Date = eomdate(IndexCardPortfVars.Year,IndexCardPortfVars.Month,'datetime');
IndexCardPortfVars = table2timetable(IndexCardPortfVars,'RowTimes','Date');
IndexCardPortfVars.RskPrem = IndexCardPortfVars.Chargeoff_Q - IndexCardPortfVars.Chargeoff_P;
IndexCardPortfVars = IndexCardPortfVars(IndexCardPortfVars.Year<2020,:);


fig1 = figure('NumberTitle', 'Off', 'Name', 'Figure 1');

figOrientation = 'portrait';
figWidth = 8.0; 
figHeight = 6.0;


set(fig1,'Units','inch');
set(fig1,'Position',[0 0 figWidth figHeight]);
movegui(fig1,'center')

set(fig1,'PaperUnits', 'centimeters');
set(fig1,'PaperSize', [figWidth figHeight]);
set(fig1,'PaperPosition',[0 0 figWidth figHeight]);
set(fig1,'PaperOrientation',figOrientation);
set(fig1,'InvertHardcopy','on');


plotData = IndexCardPortfVars;
subplot_er(2,1,1);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_1 = plot(datenum(plotData.Date),plotData.RskPrem);
set(plot_1, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
minX = min(datenum(plotData.Date));
maxX = max(datenum(plotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylim([-5 20])
ylabel('Percent')
title('Implied Charge-Off Rate – Actual Charge-Off Rate','FontWeight','normal')
legend 'off';
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);


plotData = IndexCardPortfVars;
subplot_er(2,1,2);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_2 = plot(datenum(plotData.Date),plotData.QPChrgRateRatio);
set(plot_2, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
minX = min(datenum(plotData.Date));
maxX = max(datenum(plotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylim([0 10])
ylabel('Ratio')
title('Implied/Actual Charge-Off Rate','FontWeight','normal')
legend 'off';
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);





